define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en'],
function($, validate) {
  return {
    
    init: function() {
      this.initCheckBox();
      this.initDelete();
      
      var values = $("input[name='dispatchOrderIds']:checked").val();
      if(values) {
        $("#delete-orders").attr("disabled", false);
      }else {
        $("#delete-orders").attr("disabled", true);
      }
      
    },
    
    initCheckBox: function(){
        
      $(document).on("click", "input[name='selectAll']",function(e){
         $box = $(e.currentTarget);
         $("input[name='dispatchOrderIds']").prop("checked", $box.is(':checked'));
      })
        
    },
    
    initDelete: function(e){
      
      var that = this;
      
      $("input[name='dispatchOrderIds'], input[name='selectAll']").on("change", function(e){
        var values = $("input[name='dispatchOrderIds']:checked").val();
        if(values) {
          $("#delete-orders").attr("disabled", false);
        }else {
          $("#delete-orders").attr("disabled", true);
        }
      })
      
      $(document).on("shown.bs.modal", "#dispatch-order-delete", function(e) {
        
        var $form = $(e.currentTarget).find("form");
        $("input[name='dispatchOrderIds']:checked").each(function(){
          var inputHtml = "<input type='hidden' name='dispatchOrderIds' value='"+$(this).val()+"' />";
          $form.append(inputHtml);
        })
        
        $form.shiftform({
          needLoad: false,
          success: function(el, backdrop, message) {
            that.handlerMessage(el, backdrop, message, "success");
          },
          failure: function(el, backdrop, message) {
            that.handlerMessage(el, backdrop, message, "danger");
          }
        })
      })
    },
    
    handlerMessage: function(el, backdrop, message, type) {
      
      $("#dispatch-order-delete").modal("hide");
      
      var modalHtml = '<div class="modal fade" id="dispatch-order-delete-result" tabindex="-1" role="dialog">' + 
                        '<div class="modal-dialog ms-modal-dialog" role="document">' +
                          '<div class="modal-content">' +
                            '<div class="modal-header">' +
                              '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
                              '<h4 class="modal-title">删除结果</h4>'+
                            '</div>'+
                            '<div class="modal-body">'+
                              '<p style="font-size: 14px;word-wrap: break-word;word-break: break-word;overflow: hidden;" class="text-center text-'+type+'">'+ message +'</p>'+
                            '</div>'+
                            '<div class="modal-footer">'+
                              '<button type="button" class="btn ms-btn btn-primary" data-dismiss="modal">刷新页面</button>'+
                            '</div>'+
                          '</div>'+
                        '</div>'+
                      '</div>';
      
      $(".ms-content-layout").append(modalHtml);
      $("#dispatch-order-delete-result").modal('show');
      $("#dispatch-order-delete-result").on("hidden.bs.modal", function(){
        window.location.reload();
      })
      
    }
  }
})

